/*
 * busApp.cpp
 *
 *  Created on: Jul 22, 2013
 *      Author: m
 */

#include "Bus.h"

#define REQUIRED_ARGC 4
#define ARG_BUS_NAME 1
#define ARG_ENTRANCE_BUS_STATION_NAME 2
#define ARG_EXIT_BUS_STATION_NAME 3

#define BUS_MAX_CAPACITY 2

int main ( int argc, char* argv[] )
{

    if ( argc != REQUIRED_ARGC )
    {
        std::cerr << "Cantidad de argumentos invalida." << std::endl;
        return 1;
    }

    std::string busName( argv[ARG_BUS_NAME] );
    std::string entranceBusStationName( argv[ARG_ENTRANCE_BUS_STATION_NAME] );
    std::string exitBusStationName( argv[ARG_EXIT_BUS_STATION_NAME] );

    std::clog << "busName: " << busName << std::endl;
    std::clog << "entranceBusStationName: " <<  entranceBusStationName << std::endl;
    std::clog << "exitBusStationName: " <<  exitBusStationName << std::endl;

    application::Bus bus( busName, BUS_MAX_CAPACITY );

    std::clog << "Bus Created" << std::endl;

    bus.attachStartpoint( entranceBusStationName );

    std::clog << "Startpoint Attached" << std::endl;

    bus.attachEndpoint( exitBusStationName );

    std::clog << "Endpoint Attached" << std::endl;

    while( true )
    {
        while( bus.isFull() == false )
        {
            if ( bus.loadPassenger() == false )
            {
                if( bus.isEmpty() )
                {
                    if ( bus.hasPassengersWaiting() )
                    {
                        std::clog << "Looking for passengers in the other bus station" << std::endl;
                        break;
                    }
                    else
                    {
                        std::clog << "Waiting passengers" << std::endl;
                        bus.waitPassengers();
                        std::clog << "New passenger arrived" << std::endl;
                    }
                }
                else
                {
                    std::clog << "Leaving without being full" << std::endl;
                    break;
                }
            }
        }

        if ( bus.isFull() )
        {
            std::clog << "Leaving at MAX capacity" << std::endl;
        }

        std::clog << "Departing from " << bus.getCurrentLocation() << std::endl;
        bus.travel();
        std::clog << "--------------------------------------------------------------" << std::endl;
        std::clog << "Arriving to " << bus.getCurrentLocation() << std::endl;


        std::clog << "Unloading Passengers at: " << bus.getCurrentLocation() << std::endl;
        bus.unloadPassengers();
    }

    return 0;
}


